package com.shop.bookstore.controller;

import com.shop.bookstore.entity.Book;
import com.shop.bookstore.service.BookService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    //    实现与前端交互的数据请求方法
//    1.进入模块首页时需要对数据库里所有商品数据进行查询，返回前端页面进行展示
    @Autowired
    BookService bookService;
    @Autowired
    HttpServletRequest request;

    //    1.展示数据库里已有的商品列表给前端
    @RequestMapping("/bookList")
    public String bookMain(Model model){
        Object username = request.getSession().getAttribute("username");
        if(username==null){
            return "redirect:/";
        }else{
//            调用service方法查询到商品列表
            List<Book> bookList = bookService.findAll();
            System.out.println(bookList);
//            将查询结果用model锁定
            model.addAttribute("data",bookList);
            model.addAttribute("username",username);
//            return视图
            return "book/booklist";}
    }
//    签单实现增加删除更改这些操作在这处理
    @RequestMapping("/bookAdd")
    public String bookAdd(Model model){
        Object username = request.getSession().getAttribute("username");
        if(username==null){
            return "redirect:/";
        }else{
            model.addAttribute("username",username);
            return "book/bookAdd";
        }

    }

    /**
     * 处理商品添加的方法
     * @param bookname
     * @param publishers
     * @param number
     * @param author
     * @param pricing
     * @param type
     * @return
     */
    @RequestMapping("/bookAddProcess")
    public String bookAddProcess(@RequestParam String bookname,@RequestParam String author,
                                 @RequestParam String number, @RequestParam String pricing,
                                 @RequestParam String publishers,@RequestParam String type
                                 ){
//      组装一个book实体对象
        Book book = new Book(null,bookname,author,Integer.valueOf(number),Float.valueOf(pricing),type,publishers);
        if (bookService.add(book)==1){
//        如果返回ok，那么就进入商品管理首页
            return "redirect:/book/bookList";
        }else{
            return "redirect:/book/bookList";
        }
    }
    /**
     * 更新某条商品的记录
     * @param model
     * @param id
     * @return
     */
//    处理更新商品，首先要给出一个页面显示该条记录的数据
    @RequestMapping("/bookUpdate")
    public String bookUpdate(Model model, @RequestParam String id){
        Object username = request.getSession().getAttribute("username");
        if(username==null){
            return "redirect:/";
        }else{
//        首先基于id查询出来该id记录里的所有数据信息
            System.out.println("接收到的参数id值是:" + id);
            Book book=new Book();
            book.setId(Integer.valueOf(id));
//        调用service方法查询出来该记录的数据信息
            Book bookDB = bookService.findOne(book);
//        2.将这些数据信息返回给视图
            model.addAttribute("data",bookDB);
            model.addAttribute("username",username);
//        3.返回视图文件
            return "book/bookUpdate";}
    }
}
